﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V = Science.Mathematics.VectorCalculus;

namespace VectorCalculus5Ed.Chapter2.Section5
{
    public class Example01
    {
        public Example01()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
        public void Compute()
        {
            V.Function.ToLastType<double[], double[]> f
                = new V.Function.ToLastType<double[], double[]>(funcf);
            V.Function.ToLastType<double[], double[]> g
                = new V.Function.ToLastType<double[], double[]>(funcg);
            V.Function.ToLastType<double[], double[]> h
                = new V.Function.ToLastType<double[], double[]>(funch);

            double[] x0 = { 4.0, 1.0, 1.0 };

            V.MatrixOfPartialDerivatives obj1 = new V.MatrixOfPartialDerivatives(f, x0);
            obj1.Compute();
            V.MatrixOfPartialDerivatives obj2 = new V.MatrixOfPartialDerivatives(g, x0);
            obj2.Compute();
            V.MatrixOfPartialDerivatives obj3 = new V.MatrixOfPartialDerivatives(h, x0);
            obj3.Compute();

            result += obj3.Result[0, 0].ToString() + "    ";
            result += obj3.Result[0, 1].ToString() + "    ";
            result += obj3.Result[0, 2].ToString() + "\r\n";

            V.Matrix res = (g(x0)[0] * obj1.Result - f(x0)[0] * obj2.Result) * (1.0 / g(x0)[0] / g(x0)[0]);
            result += res[0, 0].ToString() + "    ";
            result += res[0, 1].ToString() + "    "; 
            result += res[0, 2].ToString() + "\r\n";
        }
        private double[] funcf(double[] x)
        {
            double[] r = new double[1];
            r[0] = x[0]*x[0] + x[1]*x[1] + x[2]*x[2];
            return r;
        }
        private double[] funcg(double[] x)
        {
            double[] r = new double[1];
            r[0] = x[0] * x[0] + 1.0;
            return r;
        }
        private double[] funch(double[] x)
        {
            double[] r = new double[1];
            r[0] = funcf(x)[0] / funcg(x)[0];
            return r;
        }
    }
}
// -0.0276816608995446    0.117647058823692    0.117647058823692
// -0.0276816608996471    0.117647058823697    0.117647058823697
   